import Vue from 'vue'

let {debounce} = require('lodash/fp')
/***
 * 防抖指令
 *  例：<el-button v-debounce="[()=>reset(param),`click`,500,{leading:true,maxWait:500,trailing:true}]">刷新</el-button>
 *  也可简写成：<el-button v-debounce="[reset]">刷新</el-button>
 *  @see lodash.debounce https://www.lodashjs.com/docs/lodash.debounce
 */
Vue.directive('debounce', {
  inserted: function (el, binding) {
    let defaultOption={leading:true,maxWait:500,trailing:false}
    //数组解构
    let [ func ,event = "click", wait = 500,option=defaultOption] = binding.value
    el.addEventListener(event,()=>{
      debounce(func,wait,option)
    })
     /* let timer
    el.addEventListener(event, () => {
      timer && clearTimeout(timer)
      timer = setTimeout(() => func(), time)
    })*/
  }
})
